package com.sgb.packers.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersWorkOrContractorBinding;
import com.sgb.packers.entity.ContractLaborEntity;
import com.sgb.packers.viewmodel.WorkOrContractorDetailModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.ImagesAdapter;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NumberUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 王彬臣
 * @date 2021/11/24
 * @Description 包工或记工详情
 */
@Route(path = PathTeam.WorkOrContractorDetailActivity)
public class WorkOrContractorDetailActivity extends BaseActivity<WorkOrContractorDetailModel, PackersWorkOrContractorBinding> {
    @Autowired(name = RouterKey.NO, desc = "详情no")
    public String no;
    @Autowired(name = RouterKey.BOOLEAN, desc = "true 点工 false 包工")
    public boolean isWork;
    private ImagesAdapter picAdapter;
    private final List<String> imageUrls = new ArrayList<>();
    private final String CODE_COMMA = ",";

    @Override
    protected int layoutId() {
        return R.layout.packers_work_or_contractor;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(WorkOrContractorDetailModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.actionBar.setOnBackClickListener(v -> finish());
        mBinding.rvImg.setLayoutManager(new GridLayoutManager(this, 4));
        DividerDecoration.builder(this)
                .color(Color.TRANSPARENT)
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.rvImg);
        picAdapter = new ImagesAdapter(imageUrls);
        picAdapter.bindToRecyclerView(mBinding.rvImg);
        if (isWork) {
            mBinding.actionBar.setTitle("点工详情");
            mBinding.llUnit.setVisibility(View.GONE);
            mBinding.llPrice.setVisibility(View.GONE);
            mViewModel.getPointWork(no);
        } else {
            mBinding.actionBar.setTitle("包工详情");
            mBinding.llTimes.setVisibility(View.GONE);
            mBinding.llUnit.setVisibility(View.VISIBLE);
            mBinding.llPrice.setVisibility(View.VISIBLE);
            mViewModel.getWrapWork(no);
        }
        initData();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        mViewModel.ldRecords.observe(this, data -> {
            mBinding.tvProjectName.setText(data.getProjectName());
            mBinding.tvDate.setText(DateUtils.stampToDayName(String.valueOf(data.getWorkTime())));
            String overType = "1".equals(data.getWorkOvertimeType()) ? "天" : "小时";
            String overTime = "1".equals(data.getHaveWorkOvertime()) ? "0小时" : data.getWorkOvertimeDuration() + overType;
            //1按一个工,2按输入工天,3:休息,4:按小时
            switch (data.getWorkType()) {
                case "1":
                case "2":
                    mBinding.tvTimes.setText("上班" + NumberUtil.trim0(data.getWorkDuration()) + "个工  加班" + overTime);
                    break;
                case "3":
                    mBinding.tvTimes.setText("上班休息  加班" + overTime);
                    break;
                case "4":
                    mBinding.tvTimes.setText("上班" + NumberUtil.trim0(data.getWorkDuration()) + "小时  加班" + overTime);
                    break;
                default:
                    break;
            }
            if (data.getSalary() != 0) {
                mBinding.tvMoney.setText(NumberUtil.trim0(data.getSalary()) + "元");
            } else {
                mBinding.tvMoney.setText("");
                mBinding.llSalary.setVisibility(View.GONE);
            }

            mBinding.tvRecord.setText(DateUtils.stampToMinute(data.getCreateTime() + ""));
            if (!TextUtils.isEmpty(data.getRemark())) {
                mBinding.tvNote.setText(data.getRemark());
            } else {
                mBinding.tvNote.setVisibility(View.GONE);
                mBinding.title.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(data.getPicture())) {
                if (data.getPicture().contains(CODE_COMMA)) {
                    String[] picture = data.getPicture().split(CODE_COMMA);
                    Collections.addAll(imageUrls, picture);
                } else {
                    imageUrls.add(data.getPicture());
                }
                picAdapter.notifyDataSetChanged();
            } else {
                mBinding.rvImg.setVisibility(View.GONE);
                mBinding.title2.setVisibility(View.GONE);
            }
        });

        mViewModel.contractLaborEntity.observe(this, contractLaborEntity -> {
            mBinding.tvProjectName.setText(contractLaborEntity.projectName);
            mBinding.tvDate.setText(DateUtils.stampToDayName(contractLaborEntity.workTime));
            setQuantities(contractLaborEntity);

            if (!TextUtils.isEmpty(contractLaborEntity.unitPrice)) {
                mBinding.tvUnitPrice.setText(contractLaborEntity.unitPrice + "元");
            } else {
                mBinding.tvUnitPrice.setText("");
                mBinding.llPrice.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(contractLaborEntity.salary) && !"0".equals(contractLaborEntity.salary)) {
                mBinding.tvMoney.setText(contractLaborEntity.salary + "元");
            } else {
                mBinding.llSalary.setVisibility(View.GONE);
            }

            mBinding.tvRecord.setText(DateUtils.stampToMinute(contractLaborEntity.createTime));
            if (!TextUtils.isEmpty(contractLaborEntity.remark)) {
                mBinding.tvNote.setText(contractLaborEntity.remark);
            } else {
                mBinding.tvNote.setVisibility(View.GONE);
                mBinding.title.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(contractLaborEntity.picture)) {
                if (contractLaborEntity.picture.contains(CODE_COMMA)) {
                    String[] picture = contractLaborEntity.picture.split(CODE_COMMA);
                    Collections.addAll(imageUrls, picture);
                } else {
                    imageUrls.add(contractLaborEntity.picture);
                }
                picAdapter.notifyDataSetChanged();
            } else {
                mBinding.rvImg.setVisibility(View.GONE);
                mBinding.title2.setVisibility(View.GONE);
            }
        });

        mViewModel.serviceError.observe(this, aBoolean -> MToast.showToast(WorkOrContractorDetailActivity.this, "服务器出错啦~"));
    }

    @SuppressLint("SetTextI18n")
    private void setQuantities(ContractLaborEntity contractLaborEntity) {
        if (!TextUtils.isEmpty(contractLaborEntity.quantitiesUnit) && contractLaborEntity.quantities != 0) {
            switch (contractLaborEntity.quantitiesUnit) {
                case "1":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "平方米");
                    break;
                case "2":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "立方米");
                    break;
                case "3":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "吨");
                    break;
                case "4":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "米");
                    break;
                case "5":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "寸");
                    break;
                case "6":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "个");
                    break;
                case "7":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "次");
                    break;
                case "8":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "天");
                    break;
                case "9":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "块");
                    break;
                case "10":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "组");
                    break;
                case "11":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "台");
                    break;
                case "12":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "捆");
                    break;
                case "13":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "宗");
                    break;
                case "14":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "项");
                    break;
                case "15":
                    mBinding.tvUnit.setText(contractLaborEntity.quantities + "珠");
                    break;
                default:
                    break;
            }
        } else {
            mBinding.llUnit.setVisibility(View.GONE);
        }
    }
}
